//
//  LFBshoppingCartToolBarView.m
//  loveFreshBeen
//
//  Created by Feng on 2017/7/11.
//  Copyright © 2017年 feng. All rights reserved.
//

#import "LFBshoppingCartToolBarView.h"
#import "LFBClickProductView.h"
#import "LFBShoppingCartManager.h"
#import <Masonry.h>

@interface LFBshoppingCartToolBarView ()

@property (weak, nonatomic) IBOutlet UIButton *shoppingCartButton;
@property (weak, nonatomic) IBOutlet UIButton *reduceButton;
@property (weak, nonatomic) IBOutlet UIButton *increaseButton;
@property (weak, nonatomic) IBOutlet UILabel *numLabel;

//页面中当前商品的数量
@property(nonatomic,assign) NSInteger singleProductNum;
@end

@implementation LFBshoppingCartToolBarView

+ (instancetype)shoppingCartToolBarView
{
    return [[NSBundle mainBundle] loadNibNamed:@"shoppingCartToolBar" owner:nil options:nil].lastObject;
}

//从xib加载入口
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
//    //创建+ - 按钮视图 --> LFBClickProductView
//    LFBClickProductView *clickProductView = [LFBClickProductView clickProductView];
//    clickProductView.backgroundColor = [UIColor clearColor];
//    [self addSubview:clickProductView];
//    
//    [clickProductView mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//        make.centerX.offset(-60);
//        make.centerY.offset(10);
//        make.height.offset(27);
//        make.width.offset(106);
//    }];
    
    //设置红色角标
    //无法设置,此时xib加载没有成功
//    NSString *numStr = [NSString stringWithFormat:@"%zd",[LFBShoppingCartManager sharedManager].sumNums];
//    NSLog(@"%@",numStr);
//    [self.productNums setTitle:numStr forState:UIControlStateNormal];
}

//设置红色角标
- (void)setProductNums:(UIButton *)productNums
{
    _productNums = productNums;
    
}

- (void)setModel:(LFBShoppingCartModel *)model
{
    _model = model;
    
    self.numLabel.text = [NSString stringWithFormat:@"%zd",model.buyNums];
    _singleProductNum = model.buyNums;
}

//减少按钮
- (IBAction)reduceButtonAction:(id)sender {
    //购物车管理类模型数量-1
    [[LFBShoppingCartManager sharedManager] subShoppingCartProductWithProdunctModel:self.model];
    _singleProductNum = _singleProductNum > 0 ? _singleProductNum - 1 : 0;
    self.numLabel.text = [NSString stringWithFormat:@"%zd",_singleProductNum];
    
    //更改红色角标数字
    [self.productNums setTitle:[NSString stringWithFormat:@"%zd",[LFBShoppingCartManager sharedManager].sumNums] forState:UIControlStateNormal];
    
    //发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:LFBDetailProductInfoViewNumChangeNotification object:self userInfo:@{KDetailProductModel: self.model, KDetailProductNum: @(_singleProductNum)}];
    
    //角标弹簧动画
    [self setupNumAnimation];
}

//增加按钮
- (IBAction)increaseButtonAction:(id)sender {
    //购物车管理类模型数量+1
    [[LFBShoppingCartManager sharedManager] addShoppingCartProductWithProdunctModel:self.model andIsFirstTime:NO];
    _singleProductNum++;
    self.numLabel.text = [NSString stringWithFormat:@"%zd",_singleProductNum];
    
    //更改红色角标数字
    [self.productNums setTitle:[NSString stringWithFormat:@"%zd",[LFBShoppingCartManager sharedManager].sumNums] forState:UIControlStateNormal];
    
    //发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:LFBDetailProductInfoViewNumChangeNotification object:self userInfo:@{KDetailProductModel: self.model, KDetailProductNum: @(_singleProductNum)}];
    
    //角标弹簧动画
    [self setupNumAnimation];
}

- (void)setupNumAnimation
{
    [UIView animateWithDuration:0.1 animations:^{
        
        _productNums.transform = CGAffineTransformMakeScale(0.6, 0.6);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.1 animations:^{
            _productNums.transform = CGAffineTransformIdentity;
        }];
    }];
}

- (IBAction)clickShoppingCartAction:(id)sender {
    //弹出购物车
}

@end
